<?php
/**
 * @author GhostWei
 * @date Aug 12, 2012
 */
class community_review_concept extends system_concept_abstract {
	
	public function __construct($lang) {
		parent::__construct(array('lang' => $lang), array('lang = ?' => $lang));
	}
	
	public function init() {
		$this->setRowClass('community_review_row');
		$this->setModel('community_review_model', 'review');
		$this->setModelCols('review', array('id', 'lang', 'category', 'parent_id', 'passport_id', 'reply_id'));
		$this->setModelCols('review', array('title', 'message', 'is_approved', 'like', 'ip', 'creationtime'));
	}
	
	public function save(community_review_row $row) {
		$row->setParentId(intval($row->get('parent_id')));
		if(!$row->getParentId()) {
			throw new community_exception("review target is empty.");
		}
		$row->setIsApproved($row->get('is_approved') ? 1 : 0);
		if(!$row->getMessage()) {
			throw new community_exception("review message is empty.");
		}
		$row->setLike(intval($row->get('like')));
		if($row->getId()) {
			$row->update();
		} else {
			$row->setCreationtime(system_date::timestamp());
			$row->setId($this->getModel('review')->insert($row->getModelData('review')));
		}
		return $row;
	}
	
	public function delete(community_review_row $row) {
		$id = $row->getId();
		$model = new community_review_model();
		$model->delete("reply_id = '$id'");
		return parent::delete($row);
	}
	
}
